Add map_err_with()#951
Conversation
| /// Map the primary error of this parser to another value, making use of the parser state and | ||
| /// context. | ||
| /// | ||
| /// This function is useful for augmenting errors to allow them to include context in non context-free | ||
| /// languages, or provide contextual notes on possible causes. | ||
| /// | ||
| /// The output type of this parser is `O`, the same as the original parser. | ||
| /// |
There was a problem hiding this comment.
While I think this change is a positive, I am a bit worried about users relying on things like MapExtra::slice, MapExtra::span, etc. Chumsky deliberately permits parsers to leave the input in an unspecified state after an error occurs and backtracking begins (it's up to calling parsers to explicitly rewind the input if they need recovery). As such, I think we should explicitly document that these functions may not produce the output a user might expect.
There was a problem hiding this comment.
Yeah, while implementing this I was wondering what to provide to the closure. So I think you're saying that all of MapExtra is too much because some of it doesn't make much sense in case of an error?
Just span (map_error_with_state gets this at the moment though maybe computed differently), state and context then? Should it be a new struct or just passed individually to the closure? If it's the latter, this new function would be basically like the existing map_error_with_state but with one additional argument.
There was a problem hiding this comment.
I'm honestly not sure! For now, I think the easiest approach (and most consistent with the rest of the crate) is to stick with MapExtra, but document that the output of MapExtra::span and MapExtra::slice are unspecified. This certainly warrants further thought, but I don't think it should block this PR. I'll merge when the change is made to the docs, thanks.
There was a problem hiding this comment.
I added a warning. Let me know if I should change the wording.
Should I also add a deprecation note for map_err_with_state()?
zesterer
left a comment
There was a problem hiding this comment.
See the comment above. I am broadly in favour, I just think we need to nail down exactly what is and isn't specified :)
|
Thanks! |
closes #948
See discussion in the linked issue.